﻿using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable disable

namespace RandomDemo.Models
{
    public partial class RandomDataBaseContext : DbContext
    {
        public RandomDataBaseContext()
        {
        }

        public RandomDataBaseContext(DbContextOptions<RandomDataBaseContext> options)
            : base(options)
        {
        }

        public virtual DbSet<Menu> Menus { get; set; }
        public virtual DbSet<MenuGroup> MenuGroups { get; set; }
        public virtual DbSet<MenuMenuGroupMiddleTable> MenuMenuGroupMiddleTables { get; set; }
        public virtual DbSet<Record> Records { get; set; }
        public virtual DbSet<SysRole> SysRoles { get; set; }
        public virtual DbSet<SysUser> SysUsers { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
                optionsBuilder.UseSqlServer("Data Source=129.211.67.141;Initial Catalog=RandomDataBase;User ID=pu;Password=pu123");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasAnnotation("Relational:Collation", "Chinese_PRC_CI_AS");

            modelBuilder.Entity<Menu>(entity =>
            {
                entity.ToTable("Menu");

                entity.Property(e => e.MenuId).ValueGeneratedNever();

                entity.Property(e => e.MenuDetails)
                    .HasMaxLength(500)
                    .IsUnicode(false);

                entity.Property(e => e.MenuName)
                    .HasMaxLength(500)
                    .IsUnicode(false);
            });

            modelBuilder.Entity<MenuGroup>(entity =>
            {
                entity.ToTable("MenuGroup");

                entity.Property(e => e.GroupIntroduce)
                    .HasMaxLength(500)
                    .IsUnicode(false);

                entity.Property(e => e.GroupName)
                    .HasMaxLength(500)
                    .IsUnicode(false);
            });

            modelBuilder.Entity<MenuMenuGroupMiddleTable>(entity =>
            {
                entity.ToTable("MenuMenuGroupMiddleTable");

                entity.HasOne(d => d.MenuGroup)
                    .WithMany(p => p.MenuMenuGroupMiddleTables)
                    .HasForeignKey(d => d.MenuGroupId)
                    .HasConstraintName("FK_MenuMenuGroupMiddleTable_MenuGroup");

                entity.HasOne(d => d.Menu)
                    .WithMany(p => p.MenuMenuGroupMiddleTables)
                    .HasForeignKey(d => d.MenuId)
                    .HasConstraintName("FK_MenuMenuGroupMiddleTable_Menu");
            });

            modelBuilder.Entity<Record>(entity =>
            {
                entity.ToTable("Record");

                entity.Property(e => e.Time).HasColumnType("datetime");

                entity.HasOne(d => d.CreateUser)
                    .WithMany(p => p.Records)
                    .HasForeignKey(d => d.CreateUserId)
                    .HasConstraintName("FK_Record_SysUser");

                entity.HasOne(d => d.Group)
                    .WithMany(p => p.Records)
                    .HasForeignKey(d => d.GroupId)
                    .HasConstraintName("FK_Record_MenuGroup");

                entity.HasOne(d => d.SelectMenu)
                    .WithMany(p => p.Records)
                    .HasForeignKey(d => d.SelectMenuId)
                    .HasConstraintName("FK_Record_Menu");
            });

            modelBuilder.Entity<SysRole>(entity =>
            {
                entity.HasKey(e => e.RoleId);

                entity.ToTable("SysRole");

                entity.Property(e => e.RoleName)
                    .HasMaxLength(250)
                    .IsUnicode(false);
            });

            modelBuilder.Entity<SysUser>(entity =>
            {
                entity.HasKey(e => e.UserId);

                entity.ToTable("SysUser");

                entity.Property(e => e.Account)
                    .HasMaxLength(500)
                    .IsUnicode(false);

                entity.Property(e => e.Other)
                    .HasMaxLength(500)
                    .IsUnicode(false);

                entity.Property(e => e.PassWord)
                    .HasMaxLength(500)
                    .IsUnicode(false);

                entity.Property(e => e.UserName)
                    .HasMaxLength(250)
                    .IsUnicode(false);

                entity.HasOne(d => d.Role)
                    .WithMany(p => p.SysUsers)
                    .HasForeignKey(d => d.RoleId)
                    .HasConstraintName("FK_SysUser_SysRole");
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
